<template>
  <div class="updateUser">
   <el-form ref="ruleForm" :rules="rules" :model="form" label-width="120px">
    <el-form-item label="用户名" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>

      <el-form-item label="账号" prop="userName">
        <el-input v-model="form.userName" />
      </el-form-item>
      
      <el-form-item label="电话号码" prop="mobilePhone">
        <el-input v-model="form.mobilePhone" @blur="Verification"/>
        <p v-if="tipShow" style="color:red;padding: 0;margin:0px;">温馨提示：手机号码格式错误！</p>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-radio v-model="form.sex" :label="1">男</el-radio>
        <el-radio v-model="form.sex" :label="0">女</el-radio>
      </el-form-item>

      <el-form-item label="角色" prop="roleName">
        <el-select v-model="form.role" filterable placeholder="请选择角色" style="width: 50%;" @change="roleMessage">
          <el-option v-for="(item, index) in roleList" :value-key="index" :label="item.roleName" :value="item" />
        </el-select>
      </el-form-item>

      <el-form-item label="部门" prop="deptName">
        <el-select v-model="form.deptName" filterable placeholder="请选择部门" style="width: 50%;">
          <el-option v-for="(item, index) in deptmentList" :key="index" :label="item.name" :value="item.deptName" />
        </el-select>
      </el-form-item>

      <el-form-item label="生日" prop="birth">
        <el-date-picker v-model="form.birth2" @change="getDate" type="date"  placeholder="选择日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="职位" prop="postName">
        <el-input v-model="form.postName" />
      </el-form-item>

      <el-form-item label="区域" prop="areaName">
        <span>
          {{form.areaName}}
          <el-button size="mini" @click="areaShow = true;" style="padding:5px 7px;margin-left: 10px;">修改</el-button>
        </span>
        <v-distpicker v-if="areaShow" @province="onChangeProvince" @city="onChangeCity" @area="onChangeArea"></v-distpicker>
      </el-form-item>

    </el-form>

    <div style="text-align: center;">
      <el-button type="primary" @click="onSubmit('ruleForm')">确认</el-button>
      <el-button @click="onCancel">关闭</el-button>
    </div>

  </div>
</template>

<script>
import request from '@/utils/request'
import qs from 'qs';
import axios from 'axios'
import VDistpicker from 'v-distpicker'


export default {
  props: ['row'],
  components: { VDistpicker },
  data() {
    return {
      areaShow:false,
      institutionList: [],
      roleList: [],
      deptmentList:[],
      tipShow:false,
      form: {},
      buttonLoading: false,
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        institutionId: [
          { required: true, message: '请选择单位', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getUserById();
    // this.getRoleList();
    // this.getInstitutionList();
  },
  mounted() {
    this.getDeptment();
    this.getRoleList();
  },
  methods: {
    getUserById() {
      this.form = this.row;
      this.form.role = this.form.roleName;
    },
     //获取所有角色信息展示在下拉框中
    getRoleList() {
      var userId = sessionStorage.getItem("userId");
      var token = sessionStorage.getItem("token");
      axios({
        method:'post',
        url:'/sysRole/queryAllRole',
        data:qs.stringify({userId: userId, token: token})
      }).then((res) => {
        this.roleList = res.data.data;
      })
    },
     // 获取所有部门信息展示在下拉框中
    getDeptment(){
      var userId = sessionStorage.getItem("userId");
      var token = sessionStorage.getItem("token");
      axios({
        method:'post',
        url:'/sysDept/queryAllDept',
        data:qs.stringify({token:token, userId:userId})
      }).then((res) => {
        this.deptmentList = res.data.data;
      })
    },
    Verification(){
        if(!(/^1[3456789]\d{9}$/.test(this.form.mobilePhone))){ 
          this.tipShow = true;
        } else{
          this.tipShow = false;
        }
      },
    getInstitutionList() {
      request({
        url: '/institution/getInstitutionList',
        method: 'get',
        params: {}
      }).then((res) => {
        this.institutionList = res.result;
      }).catch((err) => {
        this.$message(err)
      })
    },
    roleMessage(value){
      this.form.role = value.roleName;
      this.form.roleName = value.roleName;
      this.form.roleId = value.roleId;
    },
    onSubmit(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          var userId = sessionStorage.getItem("userId");
          var userName = sessionStorage.getItem("userName");
          this.buttonLoading = true;
          this.form.token = "abc123";
          this.form.uId = userId;
          this.form.uName = userName;
          axios({
            url: '/sysUser/updateSysUser',
            method: 'post',
            data: qs.stringify(this.form)
          }).then((res) => {
            if (res.data.status == 200) {
              this.$message(res.data.data)
              this.$emit('refresh');
              this.$emit('closeDialog', false);
            } else{
              this.$message(res.data.data)
            }
          }).catch(() => {
            this.buttonLoading = false;
          })
        } else {
          this.$message({
            message: '表单验证失败',
            type: 'warning'
          })

          return false;
        }
      });
    },
    onCancel() {
      this.$emit('closeDialog', false);
    },
    getDate (date){
      this.form.birth = date; 
    },
    //获取省市区信息赋值给form
    onChangeProvince(a){
      this.form.areaName = a.value;
      this.form.areaId = a.code;
    },
    onChangeCity(a){
      this.form.areaName = this.form.areaName + a.value;
      this.form.areaId = a.code;
    },
    onChangeArea(a){
      this.form.areaName = this.form.areaName + a.value;
      this.form.areaId = a.code;
    },
    ////////////////
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>
